//----------------------------------------------------------------------------
// View 'HerhaalReceptDetails'
//----------------------------------------------------------------------------

var viewHerhaalReceptDetails = Ti.UI.createScrollView({
   contentWidth : 'auto',
   contentHeight : 'auto',
   showVerticalScrollIndicator : true,
   showHorizontalScrollIndicator : true
});

var viewLetterhead1 = Titanium.UI.createImageView({
   top : '0',
   left : '10',
   height : '50',
   width : '75',
   image : 'images/logo/Letterhead1.png'
});
var viewLetterhead2 = Titanium.UI.createImageView({
   top : '0',
   right : '0',
   height : '100',
   width : '180',
   image : 'images/logo/Letterhead2.png'
});

var viewHerhaalReceptDetailsContent = Titanium.UI.createImageView();



//----------------------------------------------------------------------------
// Dialogs
//----------------------------------------------------------------------------
var dialogBestelNogmaals = Titanium.UI.createAlertDialog({
   title :  T_HHRECEPT_DETAILS_DLG_AGAIN_MSG,
   buttonNames : [T_OK, T_CANCEL],
   cancel : 1
});
dialogBestelNogmaals.addEventListener('click', function(event) {
   if (event.index == 0) {
      alert('TODO: Herhaal recept bestellen');
   }
   else {
      log("Herhaalrecept nogmaals bestellen geannuleerd");
   }
});


//----------------------------------------------------------------------------
// Navbar Buttons
//----------------------------------------------------------------------------
var btnTerugNaarHistorie = Titanium.UI.createButton({
   title : T_CLOSE
});
btnTerugNaarHistorie.addEventListener('click', terugNaarHistorie);

var btnBestelOpnieuw = Titanium.UI.createButton({
   title : T_HHRECEPT_DETAILS_BTN_AGAIN
});
btnBestelOpnieuw.addEventListener('click', function(e) {
   dialogBestelNogmaals.show();
});


//----------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------
function getHHRecept(dt) {
   viewHerhaalReceptDetailsContent = Titanium.UI.createScrollView({
      top : '10',
      bottom: '10',
      borderRadius : '3',
      borderColor : middenblauw,
      backgroundColor : wit
   });
   if (isIphone()) {
      viewHerhaalReceptDetailsContent.setWidth('90%');
   }
   else if (isIpad()) {
      viewHerhaalReceptDetailsContent.setWidth('500');
   }

   hhreceptendb = Ti.Database.open('hhrecepten');
   var hhreceptRS = hhreceptendb.execute('SELECT * FROM hhrecepten WHERE date_time=?', dt);

   if (hhreceptRS.isValidRow()) {
      var hhreceptDetails =
       hhreceptRS.getFieldByName('initials') + ' ' + hhreceptRS.getFieldByName('name') + '\n' + 
       //hhreceptRS.getFieldByName('bdate') + '\n' + 
       hhreceptRS.getFieldByName('street') + '\n' + 
       hhreceptRS.getFieldByName('postcode') + ' ' + hhreceptRS.getFieldByName('place') + '\n' + 
       hhreceptRS.getFieldByName('phone') + '\n\n' + 
       '[Herhaalrecept]\n' + 
       'Medicijn: ' + hhreceptRS.getFieldByName('medicine') + '\n' + 
       'Dosering: ' + hhreceptRS.getFieldByName('dose') + '\n' + 
       'Aantal: ' + hhreceptRS.getFieldByName('number') + '\n' + 
       'Gebruik: ' + hhreceptRS.getFieldByName('usage') + '\n' + 
       'Apotheek: ' + hhreceptRS.getFieldByName('pharmacy');       
      
      var lblHhreceptDetails = Ti.UI.createLabel({
         top : '80',
         left : '10',
         color : middenblauw,
         textAlign : Ti.UI.TEXTALIGNMENTLEFT,
         font : {
            fontSize : 12,
            fontFamily : customFont
         },
         text : hhreceptDetails
      });

      var imgHhreceptDetails = Ti.UI.createImageView({
         top : '265',
         left: 10,
         borderColor: lichtblauw,
         borderRadius: 3,
         left: '10',
         borderColor: lichtblauw,
         borderRadius: '3',
         image : hhreceptRS.getFieldByName('image')
      });

      viewHerhaalReceptDetailsContent.add(viewLetterhead1, viewLetterhead2, lblHhreceptDetails, imgHhreceptDetails);
      viewHerhaalReceptDetails.add(viewHerhaalReceptDetailsContent);
   }
   hhreceptRS.close();
   hhreceptendb.close();
}

function terugNaarHistorie() {
   viewHerhaalReceptDetails.remove(viewHerhaalReceptDetailsContent);
   viewHerhaalReceptDetailsContent = null;
   //
   winHHRecept.remove(viewHerhaalReceptDetails);
   winHHRecept.add(viewHistorie);
   winHHRecept.leftNavButton = btnTerug;
   winHHRecept.rightNavButton = btnHistorieWissen;
   winHHRecept.setTitle('Herhaalrecepten');
}
